package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/student")
public class GetLearnVideoController extends BaseController {

	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;
	
	private static Logger logger = LoggerFactory.getLogger(GetLearnVideoController.class);

	
	/**
	 * 获取所有视频信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getCurVideo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getCurVideo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();

		try {
			//科目
			String subjectId = request.getParameter("subjectId");
			//学驾类型
			String driverType = request.getParameter("driverType");
			
			if(null != subjectId && !subjectId.isEmpty() 
					&& null != driverType && !driverType.isEmpty()){
				Map<String, Object> paramsMap = new HashMap<String, Object>();
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("imgUrl", Config.getImageStudyAddress());
				paramsMap.put("driverType",driverType);
				//获取所有视频信息
				List<Map<String, Object>> listData = bizService.getCurVideo(paramsMap);
				if(null!=listData&&listData.size()>0){
					  // 设置数据集合
					  response.setData(listData);
				}else{
					response.setErrorcode(1);
					response.setMessage("系统无课程信息！");
				}
			}else{
				response.setErrorcode(1);
				response.setReturnMsg(1, "缺少必填字段！");
			}

		} catch (Exception e) {
			logger.info("getCurVideo："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("获取视频课程列表失败!");
		} finally {
			return response.toJSON();
		}

	}
	
	/**
	 * 获取所有视频信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getVideoList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getVideoList(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();

		try {
			//用户Id
			String userId = request.getParameter("userId");
			//学员Id
			String studentId = request.getParameter("studentId");
			//科目
			String subjectId = request.getParameter("subjectId");
			//学驾类型
			String driverType = request.getParameter("driverType");
			//课程ID
			String curId = request.getParameter("curId");
			if(null != userId && !userId.isEmpty() 
					&& null != subjectId && !subjectId.isEmpty() 
					&& null != driverType && !driverType.isEmpty()
					&& null != curId && !curId.isEmpty()){
				Map<String, Object> paramsMap = new HashMap<String, Object>();
				paramsMap.put("userId", Integer.parseInt(userId));
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("imgUrl", Config.getImageStudyAddress());
				paramsMap.put("driverType",driverType);
				paramsMap.put("studentId",studentId);
				paramsMap.put("curId",curId);
				//获取所有视频信息(绑定的学员根据视频播放记录中studentId值进行区分已学未学)
				if(studentId != null && !studentId.isEmpty() && studentId != "0"){
					response = bizService.getStudentLearnVideo(paramsMap,response);
				}else{
					//(未绑定的学员根据视频播放记录中userId值进行区分已学未学)
					response = bizService.getUserLearnVideo(paramsMap,response);
				}
			}else{
				response.setErrorcode(1);
				response.setReturnMsg(1, "缺少必填字段！");
			}

		} catch (Exception e) {
			logger.info("getVideoList："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("获取视频学习信息失败!");
		} finally {
			return response.toJSON();
		}

	}
	
	/**
	 * 根据学习状态(已学、未学)获取视频学习信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getSubgroupLearnVideo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getSubgroupLearnVideo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();

		try {
			//用户Id
			String userId = request.getParameter("userId");
			//科目
			String subjectId = request.getParameter("subjectId");
			//学驾类型
			String driverType = request.getParameter("driverType");
			//视频学习类型(已学：1，未学：0)
			String studyStatus = request.getParameter("studyStatus");
			
			if(null != userId && !userId.isEmpty() 
					&& null != subjectId && !subjectId.isEmpty() 
					&& null != studyStatus && !studyStatus.isEmpty()
					&& null != driverType && !driverType.isEmpty()){
				Map<String, Object> paramsMap = new HashMap<String, Object>();
				paramsMap.put("userId", userId);
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("imgUrl", Config.getImageStudyAddress());
				paramsMap.put("studyStatus", studyStatus);
				paramsMap.put("driverType", driverType);
				//获取视频信息
				response = bizService.getVideoInfo(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}

		} catch (Exception e) {
			System.out.println("获取视频学习信息："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("获取视频学习信息失败!");
		} finally {
			System.out.println(response.toJSON());
			return response.toJSON();
		}

	}

}
